Skip to content

Conversation

kandolfp
Copy link

@kandolfp kandolfp commented Apr 11, 2025

Description

As discussed in #12167 , the behavior for multiple top-level figures together with a label is not optimal. This might be a step in the direction of allowing this feature, at least it provides a warning if this combination is used.

It works well in my setup but of course I only have a very limited view on the possibilities for crossref, figures, subfigures, multiple figures, and layouts.

The main idea is to check if labelCellContainer === false but sortedOutputs is an array with more than 1 entry.
This allows to:

  1. still define labels
    const outputLabel = label &&
         (
           labelCellContainer ||
           (Array.isArray(sortedOutputs) && (sortedOutputs.length > 1))
         ) &&
         isDisplayData(output)
         ? (label + "-" + nextOutputSuffix++)
         : label;
  2. Emit a warning if a user combines multiple top-level figures with labels.
    if (labelCellContainer === false &&
         (Array.isArray(sortedOutputs) == true && (sortedOutputs.length > 1))
       ) {
         warning("Warning: using multiple top-level figures with labels might result in unwanted behaviour.")
       }

Downside, the variable naming labelCellContainer === false implies no more labels which is than contradicted.

Extended test cases

--- 
title: "foo"
keep-tex: true
---

Example form the [docs](https://quarto.org/docs/authoring/figures.html#layout):

```{python}
#| label: fig-test
#| layout-ncol: 2
#| fig-cap: 
#|   - "Line Plot 1"
#|   - "Line Plot 2"

import matplotlib.pyplot as plt
plt.plot([1,23,2,4])
plt.show()

plt.plot([8,65,23,90])
plt.show()
```

Referencing the different Figures is not working correctly

- `@fig-test`: @fig-test
- `@fig-test-1`: @fig-test-1
- `@fig-test-1`: @fig-test-2

```{python}
#| label: fig-test2
#| fig-cap: "multiple plots"
#| fig-subcap:
#|   - "Line Plot 1"
#|   - "Line Plot 2"

import matplotlib.pyplot as plt
plt.plot([1,23,2,4])
plt.show()

plt.plot([8,65,23,90])
plt.show()
```

- `@fig-test2`: @fig-test2
- `@fig-test2-1`: @fig-test2-1
- `@fig-test2-1`: @fig-test2-2

```{python}
#| label: fig-test3
#| fig-cap: "multiple plots"

import matplotlib.pyplot as plt
plt.plot([1,23,2,4])
plt.show()
```

- `@fig-test3`: @fig-test3

```{python}
#| label: fig-test4
#| fig-cap: "multiple plots"

import matplotlib.pyplot as plt
plt.plot([1,23,2,4])
plt.show()

plt.plot([8,65,23,90])
plt.show()
```

- `@fig-test4`: @fig-test4
- `@fig-test4-1`: @fig-test4-1
- `@fig-test4-1`: @fig-test4-2

Provides the expected output and warnings.

Checklist

I have (if applicable):

Emit a warning if a user combines multiple top-level figures with
labels.

Use the same mechanism as for sub-figures to number them.
This will result in the original label without a number no existing, as
it would no make any sense to have it. Without this change the label
points to the last figure.
@kandolfp kandolfp marked this pull request as draft April 11, 2025 10:20
@kandolfp
Copy link
Author

Obviously, my testing was not rigorose enough -> converted to a draft.

@cscheid cscheid added this to the v1.8 milestone Apr 15, 2025
@cscheid cscheid modified the milestones: v1.8, v1.9 Aug 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants